<template>
  <!-- 客户供应商 -->
  <div class="app-container">
    <!-- 搜索查询 -->
    <div class="form-container">
      <el-form ref="queryForm" :inline="true" label-width="80px" :model="filters">
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="filters.enterpriseName" clearable placeholder="请输入企业名称" size="small" />
        </el-form-item>
        <el-form-item label="企业类型" prop="companyTypeId">
          <el-select v-model="filters.companyTypeId" style="width:100%" placeholder="请选择企业类型" clearable size="small">
            <el-option
              v-for="dict in enterTypeOptions"
              :key="dict.typeNum"
              :label="dict.typeName"
              :value="dict.typeNum"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            size="small"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
          />
        </el-form-item>
      </el-form>
    </div>
    <!--操作按钮-->
    <div class="btns-group">
      <el-button v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery">搜索</el-button>
      <el-button v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery">重置</el-button>
      <el-button icon="el-icon-zoom-in" size="middle" :disabled="single" @click="handleDetail">详情</el-button>
      <el-button v-hasPermi="['system:companySupplier:add']" icon="el-icon-plus" size="middle" @click="handleAdd">添加企业</el-button>
      <el-button v-hasPermi="['system:companySupplier:remove']" icon="el-icon-circle-close" size="middle" :disabled="single" @click="handleCancel">取消合作</el-button>
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData"
      :row-style="rowClass"
      style="width: 100%;"
      stripe
      class="table-list"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" />
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column prop="name" :show-overflow-tooltip="true" label="企业名称" min-width="150" align="center" />
      <el-table-column prop="sysEnterpriseType" :formatter="formartType" label="企业类型" min-width="80" align="center" />
      <el-table-column prop="creditCodePic" label="信用代码" :show-overflow-tooltip="true" min-width="150" align="center" />
      <el-table-column prop="contacts" label="联系人" min-width="80" align="center" />
      <el-table-column prop="phone" label="联系电话" min-width="60" align="center" />
      <!--      <el-table-column prop="checkStatus" label="审核状态" min-width="80" align="center">-->
      <!--        <template slot-scope="{row}">-->
      <!--          <span v-if="row.checkStatus === '0'">审核成功</span>-->
      <!--          <span v-if="row.checkStatus === '1'">审核失败</span>-->
      <!--          <span v-if="row.checkStatus === '2'">未审核</span>-->
      <!--          <span v-if="row.checkStatus === '3'">已初始化</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>
    <!-- 表格展示end-->

    <!-- 分页器 -->
    <pagination
      class="pagination-container"
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <!-- 企业信息新增修改组件start -->
    <addDialog ref="addDialog" :type-options="enterTypeOptions" />
    <!-- 企业信息新增修改组件end -->

    <!-- 企业信息详情组件start -->
    <detailDialog ref="detailDialog" />
    <!-- 企业信息详情组件end -->
  </div>
</template>

<script>
import { listCustomerSupplier, removeEnterprise } from '@/api/supplier/customerSupplier'
import { listNoPermission } from '@/api/superviseBaseInfo/typeManage/organizationType'
import { mapState } from 'vuex'
import addDialog from './components/addDialog'
import detailDialog from './components/detailDialog'
export default {
  name: 'CustomerSupplier',
  components: {
    addDialog,
    detailDialog
  },
  data() {
    return {
      // 筛选条件
      highLevelSet: false, // 高级设置显示隐藏
      filters: {
        pageNum: 1,
        pageSize: this.fristPageSize,
        enterpriseName: '', // 企业名称
        companyTypeId: '' // 企业类型
      },
      // 日期范围
      dateRange: [],
      enterTypeOptions: [], // 企业类型数组
      /* 列表 start*/
      total: 0, // 分页总条数
      single: true, // 非单个禁用
      selectRow: [], // 高亮样式
      multipleSelection: [], // 选中表格行
      loading: false, // 列表加载
      tableData: []
      /* 列表 end*/
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  created() {
    // 获取列表数据
    this.getList()
    this.getSupperType()
  },
  methods: {
    // 详情
    handleDetail() {
      this.$refs.detailDialog.handleAdd('企业详情', this.multipleSelection[0].id)
    },
    // 获取单位类型
    async getSupperType() {
      const query = { typeVal: 1 }
      const { data: { rows }} = await listNoPermission(query)
      this.enterTypeOptions = [{ typeNum: '', typeName: '全部' }, ...rows]
    },
    // 回显单位类型
    formartType(row) {
      if (JSON.stringify(row) === '{}') return
      const item = this.enterTypeOptions.find(item => {
        return item.typeNum === row.sysEnterpriseType
      })
      return item && item.typeName
    },
    // 获取列表数据
    async getList() {
      this.loading = true
      try {
        const { data: { rows, total }} = await listCustomerSupplier(this.changeDateRange(this.filters, this.dateRange))
        this.tableData = rows
        this.total = total
        // this.addTaleData()
        this.loading = false
      } catch {
        this.loading = false
      }
    },
    // list表格填充空白行
    addTaleData() {
      console.log(this.fristPageSize)
      if (this.tableData.length < this.fristPageSize) {
        const arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    /* 表格行选中事件 start */
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach((item) => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }
      if (val.length === 1) {
        this.single = false
      } else {
        this.single = true
      }
      this.selectRow = []
      this.multipleSelection = val
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    /* 表格行选中事件 end*/

    /* 按钮 Start */
    // 搜索方法
    handleQuery() {
      this.filters.pageNum = 1
      this.getList()
    },
    // 重置搜索
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 新增
    handleAdd() {
      this.$refs.addDialog.handleAdd('添加企业')
    },
    // 详情
    handleCancel() {
      this.$confirm('是否取消合作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await removeEnterprise({ ids: this.multipleSelection[0].companySupplierId })
          this.getList()
          this.msgSuccess('取消成功!')
        })
        .catch(() => {})
    }
    /* 按钮 end */
  }
}
</script>

